import { Component, OnInit, ViewChild} from '@angular/core';
import { Router } from "@angular/router";
import { CookiesService } from "ng-zorro-iop/cookie/cookies";
import { NavHeaderService } from '@global/layout/default/nav-header/nav-header.service';
import { FcsService } from "../../../service/fcs.service";
import { FcsTableComponent } from "../../../shared-component/fcs-table/fcs-table.component";
import { CommonService } from '../../../service/common.service';
import { Region } from '../../../shared-component/class/instance';

@Component({
  selector: 'app-function-list',
  templateUrl: './function-list.component.html',
  styleUrls: ['./function-list.component.less']
})
export class FunctionListComponent implements OnInit {
  @ViewChild('fcsTable', {static: true}) fcsTable: FcsTableComponent;
  applicationList = [{displayName:'全部应用',id:"all"}];
  totalCount = 0;
  groupId = null;
  studentValue;
  searchValue = '';
  errorMsg = '';
  regionName = '';
  currentRegion:Region = {
    regionCode:'',
    regionName:''
  };
  constructor(
    private router: Router,
    private fcsService: FcsService,
    private cookiesService: CookiesService,
    private commonService: CommonService,
    private cookieService:CookiesService,
    private navHeaderService: NavHeaderService) { }

  ngOnInit() {
    this.getRegionName();
    this.getListData();
  }
  
  public formChildValue(event: number) {
    this.studentValue = event;
    this.groupId = this.studentValue
    
    }
 // 标签搜索跳转传参
 refreshTagList(){
   
  const regionOn =this.cookieService.getCookie("region") || '';
  
  let url = '/tms/#/tms/resource' +'?resourceType=' +'fcs'+'&'+'region='+regionOn;
  // console.log(url)
  window.open(url);
}
  getListData(){
    this.fcsService.getGroupList().subscribe(res => {
      if(res && res.length > 0){
         res.forEach(item => {
           this.applicationList.push(item);
         })
      }
      //this.cookiesService.getCookie('ifcsListGroup')
      this.groupId = (this.applicationList.length > 0 && this.applicationList[0].id);
      this.searchFunctions();
      //this.fcsTable.getFunctionList(this.groupId);
    }, error => {
      this.errorMsg = error;
      this.fcsTable.isLoading = false;
    })
  }

  getRegionName(){
    this.commonService.getRegions().subscribe(res => {
      if(res && res['data']){
         let regions = res['data'];
         let regionId = this.navHeaderService.getCurrentRegion();
         this.regionName = this.commonService.getRegionName(regions,regionId);
         this.currentRegion['regionCode'] = regionId;
         this.currentRegion['regionName'] = this.regionName;
      }
    })
  }

  createFunction() {
    let param = {regionCode:this.currentRegion.regionCode,regionName:this.currentRegion.regionName};
    if(this.groupId && this.groupId !== 'all'){
      param['groupId'] = this.groupId;
    }
    this.router.navigate(['/fcs-manage/function/create'],{queryParams:param});
  }

  groupChange(groupId) {
    this.groupId = groupId;
    //this.cookiesService.setCookie('ifcsListGroup', this.groupId);
    this.searchFunctions();
  }

  searchFunctions(){
    this.fcsTable.getFunctionList(this.groupId, this.searchValue);
  }

  refreshFunctionList() {
    // this.groupId = 'all';
    // this.searchValue = "";
    this.fcsTable.getFunctionList(this.groupId, this.searchValue);
  }

  // ngAfterViewChecked () {
  //   let targetE = document.getElementsByClassName("cdk-overlay-container")[0];
  //   if(targetE){
  //     targetE['style'].cssText += "width:50%!important;left:0!important;right:unset;";
  //   }
  // }

}
